package com.bjnet.castdemo.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.bjnet.castdemo.CastManager;
import com.bjnet.castdemo.R;
import com.bjnet.castdemo.base.DeviceName;

public class SettingsActivity extends AppCompatActivity {

    TextView device;
    DeviceName deviceName;
    Switch pinIf;

    private int deviceNumber;
    boolean isChecked;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;

    private ConstraintLayout wifiItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_settings);
        sharedPreferences = getSharedPreferences(getResources().getString(R.string.device), Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();

        deviceName = new DeviceName(getApplicationContext());
        device = findViewById(R.id.device);
        pinIf = findViewById(R.id.pin_if);
        deviceNumber = deviceName.getNumber();
        wifiItem = findViewById(R.id.wifi);

        pinIf.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                isChecked = b;

                CastManager.getMgr().setEnablePin(isChecked);

            }
        });
    }

    public void deviceName(View v) {
        Intent intent = new Intent(this, CustomAct.class);
        startActivity(intent);
    }

    public void backPressed(View v) {
        onBackPressed();
    }

    public void wifi(View view) {
        startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility((uiOptions));
        }
    }

    public void aboutUs(View view) {
        Intent intent = new Intent(this, AboutActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();

        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility((uiOptions));

        isChecked = sharedPreferences.getBoolean(getResources().getString(R.string.sta_pin), false);
        pinIf.setChecked(isChecked);

        if (deviceName.getDeviceName() != null) {
            device.setText(deviceName.getDeviceName());
        } else {
            device.setText("BJ62_" + String.valueOf(deviceNumber));
        }

    }

    @Override
    protected void onStop() {
        editor.putBoolean(getResources().getString(R.string.sta_pin), isChecked);
        editor.commit();

        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
